//
//  oldMyAddressesListViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/3/12.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldMyAddressesListViewController.h"
#import "oldMyAddressesListTableViewCell.h"
#import "oldEditAddressViewController.h"

@interface oldMyAddressesListViewController ()<oldMyAddressesListTableViewCellDelegate>
@property (nonatomic, copy)NSArray *dataSource;
@end

static NSString *const identifier = @"oldMyAddressesListTableViewCell";

@implementation oldMyAddressesListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    self.navigationItem.title = @"我的地址";
    [self rigisterCell:identifier];
    
    UIButton *rightbutton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
    
    //[leftbutton setBackgroundColor:[UIColor blackColor]];
    
    [rightbutton setTitle:@"新增地址" forState:UIControlStateNormal];
    
    [rightbutton addTarget:self action:@selector(rightbtnTapped:) forControlEvents:UIControlEventTouchDown];
    
    UIBarButtonItem *rightitem = [[UIBarButtonItem alloc]initWithCustomView:rightbutton];
    
    self.navigationItem.rightBarButtonItem = rightitem;

}

#pragma mark - initData
- (void)initData
{
    [oldHub waiting:@"加载中.."];
    
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_ListAddress params:@{@"pageNum" : @"0", @"pageSize":@"10"} success:^(id data) {
        [oldHub hideHUD];
        [weakSelf initDataSource:data];
        [weakSelf.tableView reloadData];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

- (void)needReload
{
    [self initData];
}

- (void)initDataSource:(id)data
{
    NSMutableArray *array = [NSMutableArray array];
    
    for (int i = 0; i < [data count]; i++) {
        oldAddressInfo *address = [[oldAddressInfo alloc] initWithDictionary:data[i]];
        [array addObject:address];
    }
    
    _dataSource = array;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_dataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    oldMyAddressesListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldMyAddressesListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.addressInfo = _dataSource[indexPath.row];
    cell.index = indexPath.row;
    cell.delegate = self;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 79;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - cellfuc
-(void)cellEditAddress:(NSInteger)index
{
    oldEditAddressViewController *vc = [oldEditAddressViewController new];
    vc.addressInfo = _dataSource[index];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)rightbtnTapped:(id)sender
{
    oldEditAddressViewController *vc = [oldEditAddressViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
